package com.example.springboot.service.impl;

import com.example.springboot.dao.CommentDao;
import com.example.springboot.entity.Comment;
import com.example.springboot.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentDao commentDao;

    @Override
    public List<Comment> DownTimeComment() {
        return commentDao.DownTimeComment();
    }

    @Override
    public List<Comment> UpTimeComment() {
        return commentDao.UpTimeComment();
    }

    @Transactional
    @Override
    public boolean delCommentByRid(int repair_id) {
        if(repair_id > 0){
            try{
                boolean flag = commentDao.delCommentByRid(repair_id);
                if(flag){
                    return true;
                }else{
                    throw new RuntimeException("删除失败");
                }
            }catch (Exception e){
                throw new RuntimeException("删除失败"+e.toString());
            }
        }else{
            throw  new RuntimeException("维修id不合法");
        }
    }

    @Transactional
    @Override
    public boolean UpdateComment(Comment comments) {
        if(comments.getRepairId()>0){
            try{
                if(commentDao.UpdateComment(comments)){
                    return true;
                }
                else {
                    throw new RuntimeException("评论更新失败");
                }
            }catch (Exception e){
                throw new RuntimeException("评论更新失败"+e.toString());
            }
        }
        else {
            throw new RuntimeException("维修ID不为空，且要大于0");
        }
    }

    @Transactional
    @Override
    public boolean InsertComment(Comment comments) {
        if (comments.getRepairId()>0)
            try{
                boolean flag = commentDao.InsertComment(comments);
                if(flag)
                    return true;
                else{
                    throw new RuntimeException("添加评论失败");
                }
            }catch (Exception e){
                throw new RuntimeException("添加评论失败" + e.toString());
            }
        else
            throw new RuntimeException("维修id不能为空");
    }

    @Transactional
    @Override
    public List<Comment> queryCommentById(int repair_id) {
        if(repair_id > 0)
            return commentDao.queryCommentById(repair_id);
        else
            throw new RuntimeException("维修id错误");
    }
}
